package com.luo.controller;

import com.alibaba.fastjson.JSON;
import com.luo.model.ResultObj;
import com.luo.service.UserService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 功能：删除用户功能
 * 作者：Luo。
 * 日期：2024/2/15 19:41
 */
@WebServlet("/deleteUserByIds")
public class DeleteUserByIdsController extends HttpServlet {

    UserService userService = new UserService();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ids = req.getParameter("ids");
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        ResultObj result = new ResultObj();
        int i = 0;
        try {
            i = userService.deleteUserByIds(list);
        } catch (Exception e) {
            result.setCode("-2");
            result.setMsg("删除失败,有关联的其他数据存在");
            String jsonString = JSON.toJSONString(result);
            resp.getWriter().write(jsonString);
            resp.getWriter().close();
            return;
        }
        if (i > 0){
            result.setCode("1");
            result.setMsg("删除成功");
        }else{
            result.setCode("-1");
            result.setMsg("删除失败");
        }
        String jsonString = JSON.toJSONString(result);
        resp.getWriter().write(jsonString);
        resp.getWriter().close();
    }
}
